import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../views/Login.vue'
import DifficultySelect from '../views/DifficultySelect.vue'
import Practice from '../views/Practice.vue'
// import HistoryPage from '../views/History.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Login',
    component: Login
  },
  {
    path: '/difficulty',
    name: 'DifficultySelect',
    component: DifficultySelect,
    meta: { requiresAuth: true }
  },
  {
    path: '/practice',
    name: 'Practice',
    component: Practice,
    meta: { requiresAuth: true }
  }
]

const router = new VueRouter({
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  if (to.matched.some(record => record.meta.requiresAuth)) {
    // 这里检查用户是否已登录
    const isLoggedIn = localStorage.getItem('token')
    if (!isLoggedIn) {
      next({
        path: '/',
        query: { redirect: to.fullPath }
      })
    } else {
      next()
    }
  } else {
    next()
  }
})

export default router